home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / newsgroups / misc.20031118-20041115 / 000211_thucdat@hotmail.com_Wed Apr 14 14:31:34 2004.msg < prev    next >
Lisp/Scheme  |  2020-01-01  |  4KB  |  214 lines

  1. Path: newsmaster.cc.columbia.edu!panix!news.maxwell.syr.edu!postnews1.google.com!not-for-mail
  2. From: thucdat@hotmail.com (Dat Nguyen)
  3. Newsgroups: comp.protocols.kermit.misc
  4. Subject: Object-Oriented Programming in C-Kermit
  5. Date: 14 Apr 2004 11:01:10 -0700
  6. Organization: http://groups.google.com
  7. Lines: 198
  8. Message-ID: <6b1f50ac.0404141001.71ef5b14@posting.google.com>
  9. NNTP-Posting-Host: 24.118.27.71
  10. Content-Type: text/plain; charset=ISO-8859-1
  11. Content-Transfer-Encoding: 8bit
  12. X-Trace: posting.google.com 1081965670 8379 127.0.0.1 (14 Apr 2004 18:01:10 GMT)
  13. X-Complaints-To: groups-abuse@google.com
  14. NNTP-Posting-Date: Wed, 14 Apr 2004 18:01:10 +0000 (UTC)
  15. Xref: newsmaster.cc.columbia.edu comp.protocols.kermit.misc:14924
  16.  
  17. Yesteryear many software went object-oriented (even Pascal, Cobol and
  18. Assembler). Today Patterns is the way to go.
  19.  
  20. C-Kermit does not provide built-in OOP environment, but its dynamic
  21. evaluation of expressions can be exploited to do OOP and Patterns.
  22. The following script provides the OOP environment in C-Kermit (8.x):
  23.  
  24.     ftp://kermit.columbia.edu/kermit/scripts/ckermit/class
  25.  
  26. In this installment I use C-Kermit to implement the BankAccount class
  27. described by John Maloney in:
  28.  
  29.     http://www.squeak.org/tutorials/BankAccount.html
  30.  
  31. In Smalltalk, the BankAccount class is defined as:
  32.  
  33. Object subclass: #BankAccount
  34.  
  35.         instanceVariableNames: 'balance'
  36.  
  37.         classVariableNames: ''
  38.  
  39.         poolDictionaries: ''
  40.  
  41.         category: 'My Stuff'
  42.  
  43. In C-Kermit, with the support of the script 'class', the BankAccount
  44. class is defined as:
  45.  
  46.     class BankAccount
  47.  
  48. In C-Kermit, the instanceVariable 'balance' is created and initialized
  49. with 0 when an object of BankAccount is instantiated with the class
  50. method:
  51.  
  52.     define BankAccount::new: {
  53.         _asg \%1.balance 0
  54.         (\%1)
  55.     }
  56.  
  57. In Smalltalk, a new object of BankAccount is created as follows:
  58.  
  59.     b := BankAccount new
  60.  
  61. In C-Kermit:
  62.  
  63.     BankAccount new: b
  64.  
  65.  
  66.  
  67. The Smalltalk method to retrieve the balance is:
  68.  
  69.     balance
  70.  
  71.        "Return the balance"
  72.  
  73.        ^ balance
  74.  
  75. In C-Kermit:
  76.  
  77.     define BankAccount>>balance {
  78.         (\%1.balance)
  79.     }
  80.  
  81. and Smalltalk retrieves the balance of the account b as follows:
  82.  
  83.     b balance
  84.  
  85. In C-Kermit:
  86.  
  87.     b balance
  88.  
  89. To store that balance in a variable theBalance in C-Kermit:
  90.  
  91.     (setq theBalance (b 'balance))
  92.  
  93.  
  94.  
  95. The Smalltalk method to deposit an amount into an account is:
  96.  
  97.     deposit: amount
  98.  
  99.         balance _ balance + amount.
  100.  
  101. in C-Kermit:
  102.  
  103.     define BankAccount>>deposit: {
  104.         (++ \%1.balance \%2)
  105.         (\%1)
  106.     }
  107.  
  108. and Smalltalk deposits an amount 100 into the account b as follows:
  109.  
  110.     b deposit: 100
  111.  
  112. in C-Kermit:
  113.  
  114.     b deposit: 100
  115.  
  116.  
  117.  
  118. The Smalltalk method to withdraw an amount from an account is:
  119.  
  120.     withdraw: amount
  121.  
  122.         amount > balance ifTrue: [
  123.  
  124.                 ^ self inform: 'sorry, not enough funds']. 
  125.  
  126.         balance _ balance - amount.
  127.  
  128. in C-Kermit:
  129.  
  130.     define BankAccount>>withdraw: {
  131.         if < \m(\%1.balance) \%2 END -1 NOT ENOUGH BALANCE
  132.         (-- \%1.balance \%2)
  133.         (\%1)
  134.     }
  135.  
  136. and Smalltalk withdraws an amount 50 from the account b as follows:
  137.  
  138.     b withdraw: 50
  139.  
  140. in C-Kermit:
  141.  
  142.     b withdraw: 50
  143.  
  144. like in Smalltalk, in C-Kermit messages can be cascaded sending to an
  145. object as follows:
  146.  
  147.     ((((BankAccount 'new: 'Bell) 'deposit: 500) 'withdraw: 150) 'balance)
  148.  
  149. which creates a new account Bell, deposits 500 to it, withdraws 150
  150. from it and retrieves the balance from it.
  151.  
  152. To recap, follows is a rudimentary BankAccount application in
  153. C-Kermit:
  154.  
  155.  
  156.  
  157.     take class
  158.  
  159.     class BankAccount
  160.  
  161.     define BankAccount::new: {
  162.         _asg \%1.balance 0
  163.         (\%1)
  164.     }
  165.  
  166.     define BankAccount>>balance {
  167.         (\%1.balance)
  168.     }
  169.  
  170.     define BankAccount>>deposit: {
  171.         (++ \%1.balance \%2)
  172.         (\%1)
  173.     }
  174.  
  175.     define BankAccount>>withdraw: {
  176.         if < \m(\%1.balance) \%2 END -1 NOT ENOUGH BALANCE
  177.         (-- \%1.balance \%2)
  178.         (\%1)
  179.     }
  180.  
  181.  
  182.     BankAccount new: Rich
  183.  
  184.     (Rich 'balance)
  185.  
  186.     Rich deposit: 1000000
  187.  
  188.     (Rich 'balance)
  189.  
  190.     Rich withdraw: 1
  191.  
  192.     (Rich 'balance)
  193.  
  194.     BankAccount new: Poor
  195.  
  196.     (Poor 'balance)
  197.  
  198.     Poor deposit: 5
  199.  
  200.     (Poor 'balance)
  201.  
  202.     Poor withdraw: 11
  203.  
  204.     ((((BankAccount 'new: 'Bell) 'deposit: 500) 'withdraw: 150) 'balance)
  205.  
  206.  
  207.  
  208.  
  209. This program is object-oriented, very Smalltalk look alike. All
  210. C-Kermit particular syntaxes are wrapped in the methods of the class,
  211. and the new S-Espressions are very usefull for this style of
  212. programming.
  213.  
  214. Dat Nguyen